#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>

int gpio_get_value(char* port, unsigned int offset)
{
    //打开GPIO控制器
    struct gpiod_chip* gpio = gpiod_chip_open_by_label(port);
    if (!gpio)
    {
        return -1;
    }

    //占用GPIO引脚
    struct gpiod_line* line = gpiod_chip_get_line(gpio, offset);
    if (!line)
    {
        gpiod_chip_close(gpio);
        return -1;
    }

    //将GPIO引脚设置为输入模式
    if (gpiod_line_request_input(line, ""))
    {
        gpiod_chip_close(gpio);
        gpiod_line_release(line);
        return -1;
    }

    //获取GPIO引脚状态
    int status = gpiod_line_get_value(line);

    gpiod_line_release(line);

    gpiod_chip_close(gpio);

    return status;
}

int main()
{
    while(1)
    {
        printf("attendence: %s\n", gpio_get_value("GPIOF", 12) ? "true":"false");
        sleep(1);
    }
    
    return EXIT_SUCCESS;
}
